<?php
/**
 * Created by glq.
 * User: lenovo
 * Date: 2019/4/8
 * Time: 10:33
 */

namespace Portal\Controller;

use Common\Controller\HomebaseController;
use Portal\Model\PostsModel;
use Activity\Model\ActivityModel;

class BlueprintController extends HomebaseController {

    // 主页
    public function index()
    {
        $subjects = M('subject')
            ->alias("s")
            ->field("s.*, (select GROUP_CONCAT(name) FROM rx_category where FIND_IN_SET(id, s.categorys) order by id) as category_names")
            ->where(['status' => 1])
            ->order('sort desc, id desc')
//            ->limit(2)
            ->select();
        foreach ($subjects as &$subject_info) {
            if ($subject_info && !empty($subject_info['categorys'])) {
                $subject_info['category_names'] = array_filter(explode(',', $subject_info['category_names']));
            } else {
                $subject_info['category_names'] = [];
            }
        }
        $this->assign('subjects', $subjects);
        $this->display();
    }

    // 详情页
    public function info()
    {
        $subject_id = I('get.id');
        if ($subject_id) {
            $active = 0;
            $subject_info = M('subject')
                ->alias("s")
                ->where(['s.id' => $subject_id])
                ->field("s.*, (select GROUP_CONCAT(name) FROM rx_category where FIND_IN_SET(id, s.categorys) order by id) as category_names")
                ->find();
            if ($subject_info && $subject_info['status'] == 1) {
                if ($subject_info && !empty($subject_info['categorys'])) {
                    $subject_info['category_names'] = array_filter(explode(',', $subject_info['category_names']));
                } else {
                    $subject_info['category_names'] = [];
                }
                $this->assign('subject_info', $subject_info);

                // 文章
                if ($subject_info['article_ids']) {
                    $article_ids    = array_filter(explode(',', $subject_info['article_ids']));
                    $where          = array(
                        'p.post_type'       => PostsModel::POST_TYPE_ARTICLE,
                        'p.post_status'     => PostsModel::POST_STATUS_NORMAL,
                        'p.post_date'       => ['ELT', date("Y-m-d H:i:s")],
                        'p.id'              => ['in', $article_ids],
                    );
                    $articles = M('posts')
                        ->alias("p")
                        ->join("__USERS__ u on p.post_author = u.id", 'left')
                        ->field("p.*, (SELECT GROUP_CONCAT(name) FROM rx_label l WHERE FIND_IN_SET(l.id, p.labels) ) as label_names, u.user_nicename")
                        ->where($where)
                        ->order('p.post_order desc, p.post_date desc, id desc')
                        ->cache("subject_article_lists_" . $subject_id, C('CACHE_EXPIRE'))
//                        ->limit(3)
                        ->select();
                    foreach ($articles as $k => $v) {
                        $label_names        = array_slice(array_filter(explode(',', $v['label_names'])), 0, 1);
                        $v['label_names']   = $label_names;
                        $articles[$k]       = $v;
                    }
                    $this->assign('articles', $articles);
                    $active = $active ?: 1;
                }

                // 案例
                if ($subject_info['case_ids']) {
                    $case_ids   = array_filter(explode(',', $subject_info['case_ids']));
                    $where      = array(
                        'p.post_type'       => PostsModel::POST_TYPE_ARTICLE,
                        'p.post_status'     => PostsModel::POST_STATUS_NORMAL,
                        'p.post_date'       => ['ELT', date("Y-m-d H:i:s")],
                        'p.id'              => ['in', $case_ids],
                    );
                    $cases = M('posts')
                        ->alias("p")
                        ->join("__USERS__ u on p.post_author = u.id", 'left')
                        ->field("p.*, (SELECT GROUP_CONCAT(name) FROM rx_label l WHERE FIND_IN_SET(l.id, p.labels) ) as label_names, u.user_nicename")
                        ->where($where)
                        ->order('p.post_order desc, p.post_date desc, id desc')
                        ->cache("subject_case_lists_" . $subject_id, C('CACHE_EXPIRE'))
//                        ->limit(2)
                        ->select();
                    foreach ($cases as $k => $v) {
                        $label_names        = array_slice(array_filter(explode(',', $v['label_names'])), 0, 1);
                        $v['label_names']   = $label_names;
                        $cases[$k]       = $v;
                    }
                    $this->assign('cases', $cases);
                    $active = $active ?: 3;
                }

                // 活动
                if ($subject_info['activity_ids']) {
                    $act_model = new ActivityModel();
                    $activity_ids   = array_filter(explode(',', $subject_info['activity_ids']));
                    $activitys      = M('activity')
                        ->alias('a')
                        ->join('__AREA__ a1 on a.city_id = a1.id', 'left')
                        ->where(['a.id' => ['in', $activity_ids], 'a.status' => 1])
                        ->order('a.sort desc, a.id desc')
                        ->field('a.*, a1.areaname as city_name, (SELECT GROUP_CONCAT(name) FROM rx_label WHERE FIND_IN_SET(id, a.labels)) as label_names')
//                        ->limit(3)
                        ->select();
                    foreach ($activitys as $k => $v) {
                        if (strtotime($v['end_time']) > time() || empty($v['link_addr'])) {
                            $v['link_addr']     = U('Activity/Activity/activity_info', ['id' => $v['id']]);
                        }
                        $act_status             = get_activity_status($v);
                        $v['label_names']       = array_slice(explode(',', $v['label_names']), 0, 1);
                        $v['act_status']        = $act_status['des'];
                        $v['act_status_code']   = $act_status['status'];
                        $v['form_type_name']    = $act_model->activity_form_array[$v['act_form']];
                        $v['start_time']        = date("Y.m.d", strtotime($v['start_time']));
                        $v['end_time']          = date("Y.m.d", strtotime($v['end_time']));
                        $v['thumb']             = json_decode($v['thumb'], true);
                        $activitys[$k]          = $v;
                    }
                    $this->assign('activitys', $activitys);
                    $active = $active ?: 4;
                }

                // 商脉
                if ($subject_info['service_ids']) {
                    $service_ids    = array_filter(explode(',', $subject_info['service_ids']));
                    $services       = M('service_provider')
                        ->where(['id' => ['in', $service_ids], 'status' => 1])
//                        ->limit(4)
                        ->order('sort desc, id desc')
                        ->select();
                    foreach ($services as $k => $v) {
                        $sp_financing       = M('sp_financing')->where(['sp_id' => $v['id']])->order('time desc')->find();
                        $v['financing']     = $sp_financing;
                        $services[$k]       = $v;
                    }
                    $this->assign('services', $services);
                    $active = $active ?: 2;
                }

                $this->assign('active', $active);
                $this->display();
            } else {
                $this->error('参数错误~');
            }
        } else {
            $this->error('参数错误..');
        }
    }
}